import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Set;

public class demo03 {

    public static void test01() throws InterruptedException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
        String currWindow = driver.getWindowHandle();
        Set<String> windowHandles = driver.getWindowHandles();
        for (String window:windowHandles){
            if (!currWindow.equals(window)){
                driver.switchTo().window(window);
                break;
            }
        }
        Thread.sleep(3000);
        driver.close();
        driver.switchTo().window(currWindow);
        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("孤独摇滚");
        driver.findElement(By.cssSelector("#chat-submit-button")).click();
        driver.quit();
    }

    public static void test02() throws InterruptedException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("孤独摇滚");
        driver.findElement(By.cssSelector("#chat-submit-button")).click();
//        Thread.sleep(3000);
//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\31  > div > div._content-border_1q9is_4.content-border_r0TOp.cu-border.sc-aladdin.sc-cover-card > div > div:nth-child(3) > div > div > div > div > div.cos-swiper-content > div.cos-swiper-list.cos-swiper-transition-none > div:nth-child(1) > div > a > div > div > div.cos-image-content"))).click();
//        driver.findElement(By.cssSelector("#\\31  > div > div._content-border_1q9is_4.content-border_r0TOp.cu-border.sc-aladdin.sc-cover-card > div > div:nth-child(3) > div > div > div > div > div.cos-swiper-content > div.cos-swiper-list.cos-swiper-transition-none > div:nth-child(1) > div > a > div > div > div.cos-image-content"));
        Thread.sleep(3000);
        driver.quit();
    }

    public static void main(String[] args) throws InterruptedException {
        test02();
    }

}
